import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [

  {
    path: '/',
    name: 'home',
    component: Home,
    children: [
      {
        path: '',
        name: 'student',
        component: () => import('@/views/student/index.vue')
      },
      {
        path: 'student/add',
        name: 'studentadd',
        component: () => import('@/views/student/addStudent.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('../views/login/login.vue')
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path == '/') {
    let userInfo = localStorage.getItem('userInfo')
    if (!userInfo) {
      console.log(123);
      next({ path: '/login' })
    } else {
      next()
    }
  } else {
    next()
  }
})
export default router
